<?php

namespace App\Model;

use App\BaseModel;
use App\Helpers\Helpers;
use App\Helpers\VisitTrait;
use Illuminate\Support\Facades\DB;

class WineProduct extends BaseModel
{
    use VisitTrait;

    protected $table = "wine_product";

    protected $fillable = [
        'name',
        'chname',
        'state',
        'is_hot',
        'is_recom',
        'online',
        'online_at',
        'offline_at',
        'weight',
        'pv',
        'uv',
        'remark',
        'param',
        'detail',
        'banner',
        'attr_group_id',
        'user_id',
        'updater',
        'creator',
        'covers',
        'category_id',
        'category_name',
    ];

    public function setDetailAttribute($value)
    {
        $this->attributes['detail'] = json_encode($value);
    }

    public function getDetailAttribute($value)
    {
        return json_decode($value);
    }

    public function setParamAttribute($value)
    {
        $this->attributes['param'] = json_encode($value);
    }

    public function getParamAttribute($value)
    {
        return json_decode($value);
    }

    public function setCoversAttribute($value)
    {
        $this->attributes['covers'] = json_encode($value);
    }

    public function getCoversAttribute($value)
    {
        return json_decode($value);
    }

    //我的收藏
    public function userWantDrink()
    {
        return $this->hasOne('App\Model\WantDrink','product_id','id')
            ->where('t_user_want_drinks.user_id','=',$this->loginUser()?$this->loginUser()->id:0);
    }

    public function groupAttr()
    {
        return $this->belongsTo('App\Model\ProductAttrGroup','attr_group_id','id');

    }

    public function categories()
    {
        return $this->belongsToMany('App\Model\ProductCategory','product_category_relations','product_id',
            'category_id');
    }

    public function sku()
    {
        return $this->hasMany('App\Model\ProductSku','product_id','id');
    }

    public function showDetail()
    {
        $this->load([
            'groupAttr','groupAttr.labels','groupAttr.labels.values','sku','sku.skuAttr'
        ]);
    }

    public function beforeSave($input = null)
    {
        //切换上下时更新上下线时间
        if($this->online != $this->getOriginal('online')) {
            if ($this->online == "yes") {
                $this->online_at = Helpers::now();
            }
            if ($this->online == "no") {
                $this->offline_at = Helpers::now();
            }
        }

        return true;
    }

    public static function updateSkuCount($id)
    {
        return DB::table('wine_product')->where('id','=',$id)->update([
            'has_sku' => DB::table('product_skus')->where('product_id','=',$id)->whereNull('deleted_at')->count()
        ]);
    }

    public static function updateOrderCount($id)
    {
        return DB::table('wine_product')->where('id','=',$id)->update([
            'order_count' => DB::table('orders')->where('product_id','=',$id)->whereIn('state',[1,3,5,6])->whereNull('deleted_at')->count()
        ]);
    }

    public static function updateCollectCount($id)
    {
        return DB::table('wine_product')->where('id','=',$id)->update([
            'collect_count' => DB::table('t_user_want_drinks')->where('product_id','=',$id)->whereNull('deleted_at')->count()
        ]);
    }
}
